﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.IO;
public partial class Admin_UpdateProduct : System.Web.UI.Page
{
    ShopDataContext ds = new ShopDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            if(Request.QueryString["pCode"] != null)
            {
              string pcode =  Request.QueryString["pCode"].ToString();
              ddlCategory.Items.Add(ds.Categories.FirstOrDefault(c => c.cId == (ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pCategoryId)).cName);
              ddlCategory.Enabled = false;
              txtDes.Text =  ds.Products.FirstOrDefault(p => p.pProductCode== pcode).pDescription;
              txtDes.Enabled = false;
              txtname.Text = ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pName;
              txtname.Enabled = false;
              txtPrice.Text = ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pPrice.ToString();
              txtPrice.Enabled = false;
              txtProduct.Text = ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pProductCode;
              txtQuantity.Text = ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pQuantity.ToString();
              txtQuantity.Enabled = false;
              imgSmall.ImageUrl = "~/ImageProduct/" + ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pSmallImage;
              
              imgBig.ImageUrl = "~/ImageProduct/" + ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pBigImage;
              
              chkActive.Checked = Convert.ToBoolean( ds.Products.FirstOrDefault(p => p.pProductCode == pcode).pActive);
              
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ddlCategory.Enabled = true;
        ddlCategory.Items.Clear();
        ddlCategory.DataSourceID = "SqlDataSource2";
        txtDes.Enabled = true;
        txtname.Enabled = true;
        txtPrice.Enabled = true;
        imgSmall.Visible = false;
        imgBig.Visible = false;
        fuSmall.Visible = true;
        fuBig.Visible = true;
        txtQuantity.Enabled = true;
        btnUpdate.Enabled = false;

    }
    protected void fuSmall_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("ListProduct.aspx");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["pCode"] != null)
        {
            string pcode = Request.QueryString["pCode"].ToString();
            Category c = ds.Categories.Single(c1 => c1.cId == (ds.Products.FirstOrDefault(p1 => p1.pProductCode == pcode).pCategoryId));
            Product p = ds.Products.Single(p1 => p1.pProductCode == pcode);
            p.pCategoryId = Convert.ToInt32(ddlCategory.SelectedValue);
            p.pDescription = txtDes.Text;
            p.pName  =txtname.Text;
            p.pPrice =Convert.ToInt32(txtPrice.Text);
            p.pQuantity = Convert.ToInt32( txtQuantity.Text);
            p.pActive = chkActive.Checked;
            if(!fuSmall.FileName.Equals(""))
            {
                p.pSmallImage = fuSmall.FileName;
                try
                {
                    FileInfo TheFile = new FileInfo(Server.MapPath("~/ImageProduct/" + ds.Products.FirstOrDefault(p1 => p1.pProductCode == pcode).pSmallImage));
                    if (TheFile.Exists)
                    {
                        File.Delete(Server.MapPath("~/ImageProduct/" + ds.Products.FirstOrDefault(p1 => p1.pProductCode == pcode).pSmallImage));
                    }
                    else
                    {
                        throw new FileNotFoundException();
                    }
                }catch(FileNotFoundException ex)
                {
                    
                }
                fuSmall.SaveAs(Server.MapPath("~/ImageProduct/") + fuSmall.FileName);
                ds.SubmitChanges();
            }
            if(!fuBig.FileName.Equals(""))
            {
                p.pBigImage = fuBig.FileName;
                try
                {
                    FileInfo TheFile = new FileInfo(Server.MapPath("~/ImageProduct/" + ds.Products.FirstOrDefault(p1 => p1.pProductCode == pcode).pBigImage));
                    if (TheFile.Exists)
                    {
                        File.Delete(Server.MapPath("~/ImageProduct/" + ds.Products.FirstOrDefault(p1 => p1.pProductCode == pcode).pBigImage));
                    }
                    else
                    {
                        throw new FileNotFoundException();
                    }
                }
                catch (FileNotFoundException ex)
                {
                    
                }
                fuBig.SaveAs(Server.MapPath("~/ImageProduct/") + fuBig.FileName);
                ds.SubmitChanges();
            }
            
            ds.SubmitChanges();
            Response.Redirect("ListProduct.aspx");

        }
    }
}